<?php

namespace app\common\model\xilujob;

use fast\Tree;
use think\Cache;
use think\Model;


class JobCategory extends Model
{

    

    

    // 表名
    protected $name = 'xilujob_job_category';
    const CACHE_KEY = 'job_category_collection';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    /**
     * 获取数组格式列表数据
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getCategoryList() {
        //use cache
        $cacheKey = self::CACHE_KEY;
        $list = Cache::get($cacheKey);
        if(!$list) {
            $list = collection(static::order('weigh', 'desc')->order('id', 'asc')->select())->toArray();
            Cache::set($cacheKey, $list);
        }

        return $list;
    }

    /**
     * 获取格式化后的分类列表
     * @return array
     */
    public static function getCategoryData() {
        $tree = new Tree();
        $tree->init(static::getCategoryList());
        $categoryList = $tree->getTreeList($tree->getTreeArray(0), 'category_name');
        $categoryData = [];
        foreach ($categoryList as $k => &$v) {
            $categoryData[$v['id']] = $v['category_name'];
        }
        return $categoryData;
    }

    /*
    * @return Tree
    */
    public static function getCategoryTree() {
        $tree = new Tree();
        $tree->init(static::getCategoryList());
        return $tree;
    }


    public static function getFirstLevel() {
        $cacheKey = self::CACHE_KEY;
        $data = Cache::get($cacheKey);
        if(!$data) {
            $data = static::where('pid',0)->select();
            Cache::set($cacheKey, $data, 3600);
        }
        return $data;
    }


}
